package util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESKeyUtil
{
    public static final String DES_KEY = "des.bin";

    /** Save the specified TripleDES SecretKey to the specified file */
    public static void writeKey( SecretKey key, String fileName )
            throws IOException, NoSuchAlgorithmException,
            InvalidKeySpecException {
        // Convert the secret key to an array of bytes like this
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "DES" );
        DESKeySpec keyspec = (DESKeySpec) keyfactory.getKeySpec( key,
                DESKeySpec.class );
        byte[] rawkey = keyspec.getKey();

        // Write the raw key to the file
        FileOutputStream out = null;
        File f = null;
        try {
            f = new File( fileName );
            out = new FileOutputStream( f );
            out.write( rawkey );
        } finally {
            out.close();
        }
    }

    /** Read a TripleDES secret key from the specified file */
    public static SecretKey readKey( String fileName ) throws IOException,
            NoSuchAlgorithmException, InvalidKeyException,
            InvalidKeySpecException {
        // Read the raw bytes from the keyfile
        DataInputStream in = null;
        File f = null;
        try {
            // f = new File( FILE_DIR + fileName );
            f = new File( fileName );
            in = new DataInputStream( new FileInputStream( f ) );
            byte[] rawkey = new byte[(int) f.length()];
            in.readFully( rawkey );

            // Convert the raw bytes to a secret key like this
            DESKeySpec keyspec = new DESKeySpec( rawkey );
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "DES" );
            SecretKey key = keyfactory.generateSecret( keyspec );
            return key;
        } finally {
            in.close();
        }
    }

    public static SecretKey generateKey() throws NoSuchAlgorithmException {
        // Get a key generator for Triple DES (a.k.a DESede)
        KeyGenerator keygen = KeyGenerator.getInstance( "DES" );
        // init to a random value
        keygen.init( new SecureRandom() );
        // Use it to generate a key
        return keygen.generateKey();
    }

    public static void main( String[] args ) throws NoSuchAlgorithmException,
            InvalidKeySpecException, IOException {
        SecretKey key = generateKey();
        writeKey( key, DES_KEY );
        System.out.println( "Finished writing des key to file" );
    }
}
